package me.xue.communityhealth.admin.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.xue.communityhealth.data.Pager;
import me.xue.communityhealth.model.Information;
import me.xue.communityhealth.service.InformationService;
import me.xue.communityhealth.service.InformationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class InformationController {

    private final InformationService informationService;

    private final InformationTypeService informationTypeService;

    @Autowired
    public InformationController(InformationService informationService, InformationTypeService informationTypeService) {
        this.informationService = informationService;
        this.informationTypeService = informationTypeService;
    }

    @GetMapping("/information")
    public String all(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                      Model model) {
        PageHelper.startPage(page, 15);
        Page<Information> informationPage = informationService.pagedAll();
        PageInfo pageInfo = informationPage.toPageInfo();
        Pager pager = new Pager(pageInfo.getPages(), pageInfo.getPageNum());
        model.addAttribute("information", informationPage.getResult());
        model.addAttribute("pager", pager);
        return "information";
    }

    @GetMapping("/information/add")
    public String addPage(Model model) {
        model.addAttribute("information", new Information());
        initParams(model);
        return "information_edit";
    }

    @PostMapping("/information")
    public String add(Information information, Model model) {
        int inserted = informationService.save(information);
        if (inserted == 1) {
            return "redirect:/information";
        }
        initParams(model);
        return "information_edit";
    }

    @DeleteMapping("/information/{id:\\d+}")
    @ResponseBody
    public String delete(@PathVariable("id") Integer id) {
        int deleted = informationService.delete(id);
        if (deleted == 1) {
            return "success";
        }
        return "fail";
    }

    @GetMapping("/information/{id:\\d+}/edit")
    public String updatePage(@PathVariable("id") Integer id, Model model) {
        Information information = informationService.findOne(id);
        model.addAttribute("information", information);
        initParams(model);
        return "information_edit";
    }

    @PutMapping("/information")
    public String update(Information information, Model model) {
        int inserted = informationService.update(information);
        if (inserted == 1) {
            return "redirect:/information";
        }
        initParams(model);
        return "information_edit";
    }

    private void initParams(Model model) {
        model.addAttribute("informationTypes", informationTypeService.findAll());
    }
}
